<?php

 
 class AccountUtility
 {
		
	public static function updateCart($items,$userValues,$deleteValues,$blanketpo=0){
		
		
		foreach ($userValues as $userkey => $values){
			$indexkey = explode('|',$userkey);
			$supplier = $indexkey[0];
			$organismId = $indexkey[1];
			$orderkey = $indexkey[2];
			$qty = str_replace(",","",$values[0]);
			$items[$supplier]['organismItem'][$organismId]['order'][$orderkey] =  $qty;
		}
		
		
		foreach ($deleteValues as $userkey => $values){
			$indexkey = explode('|',$userkey);
			$supplier = $indexkey[0];
			$organismId = $indexkey[1];
			$orderkey = $indexkey[2];
			if($values > 0) {
				$items[$supplier]['organismItem'][$organismId]['order'][$orderkey] = 0;
			}
		}
		
		foreach($items as $suppkey => $values){
			$ordersum = 0;
			foreach($values['organismItem'] as $orgkey => $orderdata) {
				foreach($orderdata['order'] as $key => $value) {
					$sumvalue = str_replace(",","",$value);
					$ordersum = $ordersum + $sumvalue;
				}
				
			}
			if($ordersum == 0) {
				$items[$suppkey]['organismItem'] = null;
			}
			$items[$suppkey]['totalorder'] = $ordersum;
			$items[$suppkey]['blanketpo'] = $blanketpo;
		}
		
		//print_r($items);
		return $items;
	}
	public function saveToPayment($paymentInformation) {
		Zend_Loader::loadClass("TimeUtility");
		
		$auth = BaseAuth::getInstance();
		$org_id =  $auth->org_id;
		$payment = array();
		$status = 'Open';

		if (gettype($paymentInformation['date_paid']) == 'string') {
			if ($paymentInformation['date_paid'] == 'mm/dd/yyyy' && strlen($paymentInformation['date_paid']) < 1) {
			} else {
			  if (gettype($paymentInformation['amount']) == 'string') {
			  	$payment['paid_amount'] = floatval($paymentInformation['amount']);
				if (gettype($payment['paid_amount']) == 'float' || gettype($payment['paid_amount']) == 'integer' || gettype($payment['paid_amount']) == 'double') {					
					if ($payment['paid_amount'] > 0) {
						if (gettype($paymentInformation['wire_fees']) == 'string') {
							$payment['wire_fees'] = floatval($paymentInformation['wire_fees']);
							if (gettype($payment['wire_fees']) == 'float' || gettype($payment['wire_fees']) == 'integer'  || gettype($payment['wire_fees']) == 'double') {	
								if ($payment['wire_fees'] > 0) {
									if (gettype($paymentInformation['tt_no']) == 'string') {
										$status = 'Confirmed';
										$payment['date_paid'] = TimeUtility::convertToSQLDateFormat($paymentInformation['date_paid']);														
										$payment['tt_number'] = $paymentInformation['tt_no'];
										$payment['payment_note'] = $paymentInformation['comments'];	
										$payment['reference_no'] = $paymentInformation['reference_no'];
									}
								}
							}
						}
					}
				}
			  }
			}
		}
		if ($status == 'Confirmed') {
			Zend_Loader::loadClass("Payment");
			
			$paymentSave = new Payment();
			$paymentSave->po_no = $payment['reference_no'];
			$paymentSave->org_id = $org_id;
			$paymentSave->date_paid = $payment['date_paid'];
			$paymentSave->amount_paid = $payment['paid_amount'];
			$paymentSave->wire_fees = $payment['wire_fees'];
			$paymentSave->tt_number = $payment['tt_number'];
			$paymentSave->notes  = $payment['payment_note'];
			$paymentSave->save();
			
			Zend_Loader::loadClass("purchaseOrder");
			$po = new purchaseOrder();
			$po->loadWhere(" po_no = '".$payment['reference_no']."' AND org_id = ".$org_id);
			$po->status = $status;
			$po->save();

		}	
	}
	public function saveToPo($special_order=0,$items, $paymentInformation, $delivery_id = 0,$billing_id = 0,$resend=0){
		Zend_Loader::loadClass("purchaseOrder");
		Zend_Loader::loadClass("purchaseOrderDetail");
		Zend_Loader::loadClass("BaseControllerAuth");
		Zend_Loader::loadClass("TimeUtility");
		Zend_Loader::loadClass("Organization");
		Zend_Loader::loadClass("Referencedefault");		
		Zend_Loader::loadClass("CodesUtility");
		Zend_Loader::loadClass("Shipment");
		
		$blanketpo = 0;
		$refdefault = new Referencedefault(); 
		$organization = new Organization();
		$po = new purchaseOrder();
		$po_detail = new purchaseOrderDetail();
		$shipment = new Shipment();
		
			
		$auth = BaseAuth::getInstance();
		$org_id =  $auth->org_id;
		
		$payment = array();
		$status = 'Open';

		if (strlen($paymentInformation['amount']) > 0) {
			if (gettype($paymentInformation['date_paid']) == 'string') {
				if ($paymentInformation['date_paid'] == 'mm/dd/yyyy' && strlen($paymentInformation['date_paid']) < 1) {
				} else {
				  if (gettype($paymentInformation['amount']) == 'string') {
				  	$payment['paid_amount'] = floatval($paymentInformation['amount']);
					if (gettype($payment['paid_amount']) == 'float' || gettype($payment['paid_amount']) == 'integer' || gettype($payment['paid_amount']) == 'double') {					
						if (gettype($paymentInformation['wire_fees']) == 'string') {
							$payment['wire_fees'] = floatval($paymentInformation['wire_fees']);
							if (gettype($payment['wire_fees']) == 'float' || gettype($payment['wire_fees']) == 'integer'  || gettype($payment['wire_fees']) == 'double') {	
								if (gettype($paymentInformation['tt_no']) == 'string') {
									$status = 'Confirmed';
									$payment['date_paid'] = TimeUtility::convertToSQLDateFormat($paymentInformation['date_paid']);														
									$payment['tt_number'] = $paymentInformation['tt_no'];
									$payment['payment_note'] = $paymentInformation['comments'];	
									$payment['reference_no'] = $paymentInformation['reference_no'];
								}
							}
						}
					}
				  }
				}
			}
		}
		
		if ($status == 'Confirmed') {
			Zend_Loader::loadClass("Payment");
			
			$paymentSave = new Payment();
			$paymentSave->po_no = $payment['reference_no'];
			$paymentSave->org_id = $org_id;
			$paymentSave->date_paid = $payment['date_paid'];
			$paymentSave->amount_paid = $payment['paid_amount'];
			$paymentSave->wire_fees = $payment['wire_fees'];
			$paymentSave->tt_number = $payment['tt_number'];
			$paymentSave->notes  = $payment['payment_note'];
			$paymentSave->save();
		}
		
	
		foreach($items as $supplierkey => $values){
			
			if(!$resend) {
				$blanketpo = $values['blanketpo'];
			}
			
			//=====
			$organization->loadWhere(" org_id = ". $org_id);
			$refdefault->loadWhere(" org_id = ". $org_id . " and po_prefix = '". CodesUtility::PO_PREFIX . "'" );
			
			if($refdefault->id){
				$po_prefix = $refdefault->po_prefix;
				$next_po_no = $refdefault->next_po_no;
			} else {
				$po_prefix = CodesUtility::PO_PREFIX;
				$next_po_no = CodesUtility::PO_START_PO_NO;
			}
	
			$po->org_id = $org_id;
			$po->po_no = $po_prefix.$next_po_no;
			$po->status = $status;
			$po->po_code = $special_order;
			$po->date_order = TimeUtility::convertToSQLDateFormat(date('m/d/Y'));
			
			$po->delivery_org_address_id =  $delivery_id ? $delivery_id : $organization->primary_address_id;
			$po->billing_org_address_id = $billing_id ? $billing_id : $organization->primary_address_id;
			$po->created_by = $auth->id;
			$po->updated_by = $auth->id;
			$po->save();
			
			$refdefault->po_prefix = $po_prefix;
			$refdefault->org_id = $auth->org_id;
			$refdefault->next_po_no = $next_po_no + 1;
			$refdefault->save();
			
//			$organization->clear();
//			$organization->loadWhere(" org_id = ". $org_id );			
//			$shipment->org_id = $supplierkey;
//			$shipment->customer_org_id = $org_id;
//			$shipment->customer_org_address_id = $organization->primary_address_id;
//			$shipment->customer_org_contact_id = $organization->primary_contact_id;
//			$shipment->po_id= $po->po_id;
//			$shipment->date_created= TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
//			$shipment->date_updated= TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
//			$shipment->created_by= $auth->org_id;
//			$shipment->updated_by= $auth->org_id;
//			$shipment->save();
			
			//----			
			$count = 0;
			foreach($values['organismItem'] as $orgkey => $orderdata) {
				foreach($orderdata['order'] as $key => $value) {
					//print " key : ".$key." ";
					if($value > 0) {
						print "mike".$values['organismItem'][$orgkey]['sizes'][$key];
						$count = $count + 1;
						
						$indexkey = explode("_",$key);
						$organization->clear();
						$organization->loadWhere(" org_id = ". $supplierkey );
						$po_detail->clear();
						$po_detail->po_id = $po->po_id;
						$po_detail->supplier_org_id = $supplierkey;
						$po_detail->supplier_address_id = $organization->primary_address_id;
						$po_detail->supplier_contact_id = $organization->primary_contact_id;
						$po_detail->org_organism_id = $orderdata['org_organism_id'];
						$po_detail->order_quantity = $value;
						//$po_detail->size_code = $indexkey[0];
						$po_detail->size_code = $values['organismItem'][$orgkey]['sizes'][$key];
						$po_detail->order_amount = $orderdata['price'];
						$po_detail->created_by = $auth->id;
						$po_detail->updated_by = $auth->id;
						$po_detail->currency_mark = $orderdata['currency_mark'];
						$po_detail->po_line_no = $count * 10;	
						$po_detail->save();
						
					}
					
				}
			}
		}
		
		$po->blanket_po_no = $blanketpo;
		$po->save();
		
	}
	
	public function saveReceipt($podetailValues,$received,$doa,$reject,$tank,$org_id,$createdby){
		
		Zend_Loader::loadClass("TimeUtility");
		Zend_Loader::loadClass("Inventory");
		Zend_Loader::loadClass("OrganismAttribute");
		Zend_Loader::loadClass("Receipt");
		Zend_Loader::loadClass("ReceiptReject");
		Zend_Loader::loadClass("CartUtility");
		Zend_Loader::loadClass("OrganizationOrganism");
		
		$cart = CartUtility::getReasonCart();
		$items = $cart->items;
		
		$Fcart = CartUtility::getFishermanCart();
		$Fitems = $Fcart->items;
		
		//echo "<pre>";print_r($Fitems);echo "</pre>";die();
		
		$receipt = new Receipt();
		$inventory = new Inventory();
		$org_attribute = new OrganismAttribute();
		$receiptReject = new ReceiptReject();
		$organizationOrganism = new OrganizationOrganism();
		
	//echo "<pre>"; print_r($podetailValues); echo "</pre>"; die();
		
		foreach($podetailValues as $key => $value) {
			
			$org_attribute->clear();
			$org_attribute->loadWhere(" organism_id = ".$value['organism_id'] . " and size_code = '". $value['size_code'] ."'" );
			$qtyr = 0;
			if($received[$value['po_detail_id']] > 0) {
				
				
				$organizationOrganism->clear();
				$organizationOrganism->loadWhere(" org_id = ". $org_id . " and organism_id = ". $value['organism_id']);
				
				if( $organizationOrganism->org_organism_id ) {
					$qtyr = $received[$value['po_detail_id']] - $doa[$value['po_detail_id']] - $reject[$value['po_detail_id']];
					if(isset($tank[$value['po_detail_id']])) {
						$tankid = $tank[$value['po_detail_id']];
					} else {
						$tankid = 0;
					}
					$inventory->clear();
					$inventory->loadWhere(" org_id = ". $org_id . " and tank_id = ".  $tankid . " and org_organism_id = ". $organizationOrganism->org_organism_id . " and organism_attribute_id = ". $org_attribute->organism_attribute_id );
					
					
					
					$inventory->org_id = $org_id;
					$inventory->tank_id = $tank[$value['po_detail_id']];
					$inventory->org_organism_id = $organizationOrganism->org_organism_id; //$value['org_organism_id'];
					$inventory->organism_attribute_id = $org_attribute->organism_attribute_id;
					if($inventory->inventory_id) {
						$inventory->quantity_on_hand =  $qtyr + $inventory->quantity_on_hand;					
						$inventory->total_quantity_received = $qtyr + $inventory->total_quantity_received;
						$inventory->stock_amount = $value['order_amount'] * $qtyr + $inventory->stock_amount;
					} else {
						$inventory->quantity_on_hand =  $qtyr;
						$inventory->total_quantity_received = $qtyr;
						$inventory->stock_amount = $value['order_amount'] * $qtyr;
					}
					
					$inventory->unit = $value['unit'];
					
					$inventory->currency_mark = $value['currency_mark'];
					$inventory->last_transaction_date = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
					$inventory->last_transaction_source = 'PO';
					$inventory->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
					$inventory->created_by = $createdby;
					$inventory->save();
					
					if( count($Fitems[$value['po_detail_id']])  > 0 ) { 
					
						foreach( $Fitems[$value['po_detail_id']] as $fkey => $fvalue )  {
							$receipt->clear();
							$receipt->source = 'PO';
							$receipt->org_id = $org_id;
							$receipt->reference_id = $value['po_detail_id'];
							$receipt->supplier_org_id = $value['supplier_org_id'];
							$receipt->supplier_org_address_id = $value['supplier_address_id'];
							$receipt->supplier_org_contact_id = $value['supplier_contact_id'];
							$receipt->date_receipt = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
							$receipt->time_receipt = TimeUtility::convertToSQLDateFormat(date('H:i:s'));
							$receipt->supplier_fisher_id = $fkey;
							$receipt->org_organism_id = $value['org_organism_id'];
							$receipt->received_quantity =$fvalue['qty'];
							$receipt->doa_quantity = $fvalue['doa'];
							$receipt->reject_quantity = $fvalue['reject'];
							$receipt->reason_reject = 0;
							$receipt->inventory_id = $inventory->inventory_id;
							$receipt->received_amount = $fvalue['qty'] * $value['order_amount'];
							$receipt->currency_mark = $value['currency_mark'];
							$receipt->unit = $value['unit'];
							$receipt->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
							$receipt->created_by = $createdby;
							$receipt->save();
							
							//saving rejec reason
							foreach($fvalue['rejectreason'] as $rkey => $rvalue){
								if($rvalue > 0) {
									$receiptReject->clear();
									$receiptReject->receipt_id = $receipt->receipt_id;
									$receiptReject->org_id = $org_id;
									$receiptReject->reject_quantity = $rvalue;
									$receiptReject->reject_reason = $rkey;
									$receiptReject->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
									$receiptReject->created_by = $createdby;
									$receiptReject->save();
								}
							}
							
						}
						$Fitems[$value['po_detail_id']] = array();
						
					} else {
					
						$receipt->clear();
						$receipt->source = 'PO';
						$receipt->org_id = $org_id;
						$receipt->reference_id = $value['po_detail_id'];
						$receipt->supplier_org_id = $value['supplier_org_id'];
						$receipt->supplier_org_address_id = $value['supplier_address_id'];
						$receipt->supplier_org_contact_id = $value['supplier_contact_id'];
						$receipt->date_receipt = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
						$receipt->time_receipt = TimeUtility::convertToSQLDateFormat(date('H:i:s'));
						$receipt->org_organism_id = $value['org_organism_id'];
						$receipt->received_quantity =$received[$value['po_detail_id']];
						$receipt->doa_quantity = $doa[$value['po_detail_id']];
						$receipt->reject_quantity =$reject[$value['po_detail_id']];
						$receipt->reason_reject = 0;
						$receipt->inventory_id = $inventory->inventory_id;
						$receipt->received_amount = $qtyr * $value['order_amount'];
						$receipt->currency_mark = $value['currency_mark'];
						$receipt->unit = $value['unit'];
						$receipt->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
						$receipt->created_by = $createdby;
						$receipt->save();
						
						//saving rejec reason
						foreach($items[$value['po_detail_id']] as $rkey => $rvalue){
							if($rvalue > 0) {
								$receiptReject->clear();
								$receiptReject->receipt_id = $receipt->receipt_id;
								$receiptReject->org_id = $org_id;
								$receiptReject->reject_quantity = $rvalue;
								$receiptReject->reject_reason = $rkey;
								$receiptReject->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y H:i:s'));
								$receiptReject->created_by = $createdby;
								$receiptReject->save();
							}
						}
					}
					$items[$value['po_detail_id']] = array();
				}	
			}
		}		
		$cart->items = $items;
		$Fcart->items = $Fitems;
	}
	
	public function updateDispatch($qty) {
		
		
		
		$newitems = array();
		foreach($qty as $key => $value) {
			$newitems[$key] = $value;
		}
		
		return $newitems;
	}
	
	public function saveDispatch($items,$pohead,$param){
	
	
		Zend_Loader::loadClass("Shipment");
		Zend_Loader::loadClass("ShipmentDetail");
		Zend_Loader::loadClass("TimeUtility");
		Zend_Loader::loadClass("Organization");
		Zend_Loader::loadClass("CartUtility");
		Zend_Loader::loadClass("Inventory");
		
		$cart = CartUtility::getDispatchPoCart();
		$poitems = $cart->items;
		
		//print_r($poitems);die();
		
		$inventory = new Inventory();
		
		$customer = new Organization();
		$customer->loadWhere(" org_id = ". $param['customerId']);
		
		$organization = new Organization();
		$organization->loadWhere(" org_id = ". $param['org_id']);
		
		
		$shipment = new Shipment();
		
		//print_r($items);die();
		
		if( strlen($param['poid']) == 0 )  {
			
			// create tmp PO
			Zend_Loader::loadClass("Referencedefault");
			Zend_Loader::loadClass("CodesUtility");
			$refdefault = new Referencedefault(); 
			$refdefault->loadWhere(" org_id = ". $param['org_id'] . " and po_prefix = '". CodesUtility::TMP_PO_PREFIX . "'" );
		
			if($refdefault->id){
				$po_prefix = $refdefault->po_prefix;
				$next_po_no = $refdefault->next_po_no;
			} else {
				$po_prefix = CodesUtility::TMP_PO_PREFIX;
				$next_po_no = CodesUtility::PO_START_PO_NO;
			}
				$refdefault->po_prefix = CodesUtility::TMP_PO_PREFIX;
				$refdefault->next_po_no = $next_po_no + 1;
				$refdefault->org_id = $param['org_id'];
				$refdefault->save();
			// save to po	
			Zend_Loader::loadClass("purchaseOrder");
			Zend_Loader::loadClass("purchaseOrderDetail");
			Zend_loader::loadClass("Organism");
			Zend_Loader::loadClass("OrganizationOrganism");
			$newpohead = new purchaseOrder();
			
			
			
			$newpohead->po_no = CodesUtility::TMP_PO_PREFIX.$next_po_no;
			$newpohead->org_id = $param['customerId'];
			$newpohead->status = 'Close';
			$newpohead->date_order = TimeUtility::convertToSQLDateFormat(date('m/d/y'));
			$newpohead->delivery_org_address_id = $customer->primary_address_id;
			$newpohead->billing_org_address_id = $customer->primary_address_id;
			$newpohead->created_by = $param['userId'];
			
			$newpohead->save();
			$count = 0;
			$podetails = new purchaseOrderDetail();
			$organism = new Organism();
			$organizationOrganism = new OrganizationOrganism();
			
			foreach($items as $key => $value) {
				$count = $count + 1;
				
				$keyArray = explode("_",$key);
				$organism->clear();
				$organism->loadWhere("moid = ". $keyArray[1]);
				$organismValue = $organism->toArray();
				if($organismValue['organism_id']) {
					
					$organizationOrganism->clear();
					$organizationOrganism->loadWhere(" organism_id = ". $organismValue['organism_id'] . " and org_id = ". $param['org_id'] );
					
					$podetails->po_id = $newpohead->po_id;
					$podetails->po_line_no = $count * 10;
					$podetails->supplier_org_id = $param['org_id'];
					$podetails->supplier_address_id = $organization->primary_address_id;
					$podetails->supplier_contact_id = $organization->primary_contact_id;
					$podetails->org_organism_id = $organizationOrganism->org_organism_id;
					$podetails->order_quantity = $value;
					$podetails->size_code = $keyArray[2];
					$podetails->order_amount = $organizationOrganism->base_price_sale;
					$podetails->unit = $organismValue['sale_unit'];
					$podetails->currency_mark = $organismValue['currency_mark'];
					$podetails->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y'));
					$podetails->created_by = $param['userId'];
					$podetails->updated_by = $param['userId'];
					$podetails->save();
					
					$poitems[$count] = array('po_detail_id'=>$podetails->po_detail_id,'moid'=>$keyArray[1],'size_code'=>$keyArray[2],'order_amount'=>$organizationOrganism->base_price_sale,'order_quantity'=>$value,'balance'=>$value);
					
				}
				
			}
		
			//print_r($poitems);die();
			$param['poid'] = $newpohead->po_id;
			
		}
		
		
		
		
		$shipment->clear();
		$shipment->org_id = $param['org_id'];
		$shipment->po_id = $param['poid'];
		$shipment->date_shipment = TimeUtility::convertToSQLDateFormat(date('m/d/y'));
		$shipment->time_shipment = TimeUtility::convertToSQLDateFormat(date('H:i:s'));
		$shipment->customer_org_id = $param['customerId'];
		$shipment->customer_org_address_id = $customer->primary_address_id;
		$shipment->customer_org_contact_id = $customer->primary_contact_id;
		$shipment->currency_mark = $organization->currency_mark;
		$shipment->ship_method_id = $pohead['transport'];
		$shipment->carrier_code = $pohead['carriercode'];
		$shipment->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y'));
		$shipment->created_by = $param['userId'];
		$shipment->save();
		
		$shipmentDetail = new ShipmentDetail();
		$totalAmount = 0;
		$totalQty = 0;
		foreach($items as $key => $value) {
			$shipmentDetail->clear();
			$keyArray = explode("_",$key);
			
			foreach($poitems as $pokey => $povalue) {
				if($povalue['moid'] == $keyArray[1] && $povalue['size_code'] == $keyArray[2] ) {
					$shipmentDetail->po_line_no = $povalue['po_detail_id'];
					$shipmentDetail->ship_amount = $value * $povalue['order_amount'];
					$totalAmount = $totalAmount + ($value * $povalue['order_amount']);
					$totalQty = $totalQty + $value;
					
					$inventory->clear();
					$inventory->loadWhere(" inventory_id = ". $keyArray[0] );
					$inventory->quantity_on_hand = $inventory->quantity_on_hand - $value;
					$inventory->stock_amount = $inventory->stock_amount - $shipmentDetail->ship_amount;
					$inventory->total_quantity_withdrawn = $inventory->total_quantity_withdrawn + $value;
					$inventory->save();
					
					break;
				}
			}
			
			
			
			$shipmentDetail->shipment_id = $shipment->shipment_id;	
			$shipmentDetail->source_tank_inventory_id = $keyArray[0];
			$shipmentDetail->ship_quantity = $value;
			$shipmentDetail->currency_mark = $customer->currency_mark;
			$shipmentDetail->date_created = TimeUtility::convertToSQLDateFormat(date('m/d/y'));
			$shipmentDetail->created_by = $param['userId'];
			$shipmentDetail->save();
		}
		
		$shipment->total_amount = $totalAmount;
		$shipment->total_quantity = $totalQty;
		$shipment->save();
		
		$cart->items = null;
		
	}
	
	public function getDashboard($type){
		Zend_Loader::loadClass("purchaseOrders");
		Zend_Loader::loadClass("Organizations");
		
		$auth = BaseAuth::getInstance();
		// get the count of po for dispatch
		
		$po = new purchaseOrders();
		$dispatch = 0;
		$customer = new purchaseOrders();
		$organizationValue = $customer->getCustomerByOrg($auth->org_id);
		//print_r($organizationValue);
		foreach ( $organizationValue as $key => $value) {
			$customerid = $value['org_id'];
			if($customerid) {
				$po->clear();
				$poValues = $po->getCustomerPoByOrg($auth->org_id,$customerid);
				$dispatch = count($poValues) + $dispatch;
			}
		}
		
		$data['dispatch'] = $dispatch;
		
		
		$receipts = 0;
		$supplier = new purchaseOrders();
		$po = new purchaseOrders();
		$supplierValues = $supplier->getSupplierByOrg($auth->org_id);
		//print_r($supplierValues);
		foreach($supplierValues as $key => $value){
			$supplier_org_id = $value['org_id'];
			$poValues = $po->getSupplierPoByOrg($auth->org_id,$supplier_org_id);
			$receipts = count($poValues) + $receipts;
		}
		
		$data['receipts'] =$receipts;
		
		return $data;
	}
	
	public function savefishermanCart($podetailid,$qty,$doa,$reject) {
		Zend_Loader::loadClass("CartUtility");
		
		$cart = CartUtility::getFishermanCart();
		$items = $cart->items;
		
		foreach($qty as $key => $value) {
			if($value > 0 || $doa[$key] > 0 || $reject[$key] > 0) {
				$items[$podetailid][$key]['qty'] = $value;
				$items[$podetailid][$key]['doa'] = $doa[$key];
				$items[$podetailid][$key]['reject'] = $reject[$key];
				if($reject[$key] == 0) {
					$items[$podetailid][$key]['rejectreason'] = array();
				}
			} else {
				$items[$podetailid][$key] = array();
			}
		}
		$data = array();
		foreach($items[$podetailid] as $key => $value) {
			if(count($value) > 0) {
				$data[$key] = $value;
			}
		}
		$items[$podetailid] = $data;
		
		$cart->items = $items;
		
	}
	
	public function savefishermanReasonCart($podetailid,$fisherid,$reason){
		Zend_Loader::loadClass("CartUtility");
		
		$cart = CartUtility::getFishermanCart();
		$items = $cart->items;
		
		$items[$podetailid][$fisherid]['rejectreason'] = $reason;
		
		$cart->items = $items;
		
		
	}
	
	public function getUserType($user_type_id){
	
		Zend_loader::loadClass("UserTypes");
		Zend_loader::loadClass("CodesUtility");
		
		$userTypes = new UserTypes();
		$userTypesArray = $userTypes->getUserTypesArray("id", "code");
		$org_type = $userTypesArray[$user_type_id];
		
		return $org_type;
	}
	
	public function saveexportOrder($head,$detail,$userid) {
	
	
	
		Zend_Loader::loadClass("purchaseOrder");
		Zend_Loader::loadClass("purchaseOrderDetail");
		Zend_Loader::loadClass("purchaseOrderReference");
		
		$order = new purchaseOrder();
		$orderDetail = new purchaseOrderDetail();
		$poref = new purchaseOrderReference();
		$order->setupPrimaryKey("po_id");
		if($head) {
			foreach($head as $key => $value){
				$order->clear();
				$order->po_no = $value[1];
				$order->org_id = $value[2];
				$order->status = $value[3];
				$order->date_order = $value[4];
				$order->po_code = $value[5];
				$order->commission = $value[6];
				$order->sales_tax = $value[7];
				$order->remarks = $value[8];
				$order->blanket_po_no = $value[9];
				$order->allocation_po_no = $value[10];
				$order->delivery_org_address_id = $value[11];
				$order->billing_org_address_id = $value[12];
				$order->currency_mark = $value[13];
				$order->date_created = $value[14];
				$order->date_updated = $value[15];
				$order->created_by = $value[16];
				$order->updated_by = $value[17];
				$order->save();
				
				$po_id = $order->po_id;
				if($detail[$key]) {
					//print_r($detail[$key]);die();
					foreach($detail[$key] as $dkey => $dvalue) {
					
						$orderDetail->clear();
						$orderDetail->po_id = $po_id;
						$orderDetail->po_line_no = $dvalue[2];
						$orderDetail->supplier_org_id = $dvalue[3];
						$orderDetail->supplier_address_id = $dvalue[4];
						$orderDetail->supplier_contact_id = $dvalue[5];
						$orderDetail->fishing_area_id = $dvalue[6];
						$orderDetail->org_organism_id = $dvalue[7];
						$orderDetail->order_quantity = $dvalue[8];
						$orderDetail->gender = $dvalue[9];
						$orderDetail->age = $dvalue[10];
						$orderDetail->size_code = $dvalue[11];
						$orderDetail->order_amount = $dvalue[12];
						$orderDetail->unit = $dvalue[13];
						$orderDetail->date_due = $dvalue[14];
						$orderDetail->currency_mark = $dvalue[15];
						$orderDetail->date_created = $dvalue[16];
						$orderDetail->date_updated = $dvalue[17];
						$orderDetail->created_by = $dvalue[18];
						$orderDetail->updated_by = $dvalue[19];
						$orderDetail->save();
						
						$poref->clear();
						$poref->po_id = $value[0];
						$poref->reference_id = $dvalue[0];
						$poref->new_po_id = $po_id;
						$poref->new_reference_id = $orderDetail->po_detail_id;
						$poref->user_id = $userid;
						$poref->save();
						
					}
				}
			}
		}
	}
	
	public function saveexportReceipt($head,$detail,$userid) {
	// add to inventory
		Zend_Loader::loadClass("purchaseOrderReference");
		Zend_Loader::loadClass("Receipt");
		Zend_Loader::loadClass("Inventory");
		Zend_Loader::loadClass("ReceiptReference");
		
		$rref = new ReceiptReference();
		
		$inventory = new Inventory();
		$receipt = new Receipt();
		$receiptReject = new ReceiptReject();
		$po = new purchaseOrderReference();
		if($head) {
			foreach($head as $key => $value) {
				
				$receipt->clear();
				
				$po->clear();
				$po->loadWhere(" reference_id = ". $value[4] . " and user_id = ". $userid);
				
				$receipt->source = $value[2];
				$receipt->org_id = $value[3];
				$receipt->reference_id = $po->new_reference_id;
				$receipt->supplier_org_id = $value[5];
				$receipt->supplier_org_address_id = $value[6];
				$receipt->supplier_fisher_id= $value[7];
				$receipt->supplier_org_contact_id = $value[8];
				$receipt->document_no = $value[9];
				$receipt->date_receipt  = $value[10];
				$receipt->time_receipt  = $value[11];
				$receipt->remarks  = $value[12];
				$receipt->org_organism_id  = $value[13];
				$receipt->received_quantity  = $value[14];
				$receipt->doa_quantity  = $value[15];
				$receipt->reject_quantity  = $value[16];
				$receipt->reason_reject  = $value[17];
				$receipt->inventory_id  = $value[18];
				$receipt->received_amount  = $value[19];
				$receipt->currency_mark  = $value[20];
				$receipt->unit  = $value[21];
				$receipt->date_created   = $value[22];
				$receipt->date_updated   = $value[23];
				$receipt->created_by   = $value[24];
				$receipt->updated_by    = $value[25];
				$receipt->salesinvoiceno    = $value[27];
				$receipt->save();
				
				$rref->clear();
				$rref->receipt_id = $value[1];
				$rref->new_receipt_id = $receipt->receipt_id;
				$rref->user_id = $userid;
				$rref->save();
				
				$newreceipt_id = $receipt->receipt_id;
				IF(isset($detail[$key])) {
					foreach($detail[$key] as $dkey => $dvalue) {
						$receiptReject->clear();
						$receiptReject->receipt_id = $newreceipt_id;
						$receiptReject->org_id = $dvalue[2];
						$receiptReject->reject_quantity = $dvalue[3];
						$receiptReject->reject_reason = $dvalue[4];
						$receiptReject->notes = $dvalue[5];
						$receiptReject->date_created = $dvalue[6];
						$receiptReject->date_updated = $dvalue[7];
						$receiptReject->created_by  = $dvalue[8];
						$receiptReject->updated_by = $dvalue[9];
						$receiptReject->supplier_fisher_id = $dvalue[10];
						$receiptReject->save();	
					}
				}
				
				$inventory->clear();
				$inventory->loadWhere(" org_id = ". $value[3] . " and tank_id = ".  $value[28] . " and org_organism_id = ". $value[29] . " and organism_attribute_id = ". $value[30] );
				
				$qtyr = $value[14] - $value[15] - $value[16];
				$amount = $value[19]/$value[14];
				
				if($inventory->inventory_id) {
						$inventory->quantity_on_hand =  $qtyr + $inventory->quantity_on_hand;					
						$inventory->total_quantity_received = $qtyr + $inventory->total_quantity_received;
						$inventory->stock_amount = $amount * $qtyr + $inventory->stock_amount;
				} else {
						$inventory->quantity_on_hand =  $qtyr;
						$inventory->total_quantity_received = $qtyr;
						$inventory->stock_amount = $amount * $qtyr;
				}
				
				
				$inventory->org_id = $value[3];
				$inventory->tank_id = $value[28];
				$inventory->org_organism_id = $value[29];
				$inventory->organism_attribute_id =  $value[30];
				$inventory->save();
				
				$receipt->inventory_id = $inventory->inventory_id;
				$receipt->save();
				
			}
		}
	}
	
	public function saveexportShipment($head,$detail,$userid,$ref){
		Zend_Loader::loadClass("Shipment");
		Zend_Loader::loadClass("ShipmentDetail");
		Zend_Loader::loadClass("purchaseOrderReference");
		Zend_Loader::loadClass("Inventory");
		Zend_Loader::loadClass("purchaseOrderDetail");
		
		$shipment = new Shipment();
		$shipmentDetail = new ShipmentDetail();
		$poref = new purchaseOrderReference();
		$podetail = new purchaseOrderDetail();
		$inventory = new Inventory();
		if($head) {
			foreach($head as $key => $value){
				$shipment->clear();
				$shipment->org_id = $value[1];
				$shipment->po_id = $value[2];
				$shipment->date_shipment = $value[3];
				$shipment->time_shipment = $value[4];
				$shipment->description = $value[5];
				$shipment->packs = $value[6];
				$shipment->total_weight = $value[7];
				$shipment->total_quantity = $value[8];
				$shipment->trace_document_id = $value[9];
				$shipment->customer_org_id = $value[10];
				$shipment->customer_org_address_id = $value[11];
				$shipment->customer_org_contact_id = $value[12];
				$shipment->shipping_charges = $value[13];
				$shipment->shipping_tax = $value[14];
				$shipment->total_amount = $value[15];
				$shipment->currency_mark = $value[16];
				$shipment->invoiced = $value[17];
				$shipment->ship_method_id = $value[18];
				$shipment->preferred_mode = $value[19];
				$shipment->carrier_code = $value[20];
				$shipment->date_created = $value[21];
				$shipment->date_updated = $value[22];
				$shipment->created_by = $value[23];
				$shipment->updated_by = $value[24];
				$shipment->save();
				
				foreach($detail[$key] as $dkey => $dvalue) {
					$refid = 0;
					$poref->clear();
					$poref->loadWhere(" reference_id = ". $dvalue[2] . " and user_id = ". $userid);
					if(isset($poref->new_reference_id) && $poref->new_reference_id > 0) {
						$refid = $poref->new_reference_id;
					} else {
						$refid = $podetail->getpodetailId($ref[$dvalue[2]][0],$value[1],$ref[$dvalue[2]]);
						
					}
				
					$shipmentDetail->clear();
					$shipmentDetail->shipment_id = $shipment->shipment_id;
					$shipmentDetail->po_line_no = $refid;
					$shipmentDetail->source_tank_inventory_id = $dvalue[3];
					$shipmentDetail->ship_quantity = $dvalue[4];
					$shipmentDetail->ship_amount = $dvalue[5];
					$shipmentDetail->currency_mark = $dvalue[6];
					$shipmentDetail->date_created = $dvalue[7];
					$shipmentDetail->date_updated = $dvalue[8];
					$shipmentDetail->created_by = $dvalue[9];
					$shipmentDetail->updated_by = $dvalue[10];
					$shipmentDetail->save();
					
					$inventory->clear();
					$inventory->loadWhere(" tank_id = ". $dvalue[12] . " and org_organism_id = ". $dvalue[13] . " and organism_attribute_id = ". $dvalue[14]);
					$inventory->quantity_on_hand = $inventory->quantity_on_hand - $dvalue[4];
					$inventory->stock_amount = $inventory->stock_amount - $dvalue[5];
					$inventory->total_quantity_withdrawn = $inventory->total_quantity_withdrawn + $dvalue[4];
					$inventory->save();
					
				}
				
				$shipment->save();
			}
		}
	}
	
	public function saveexportPerformance($head,$detail,$userid) {
		
		Zend_Loader::loadClass("SupplierPerformance");
		Zend_Loader::loadClass("SupplierPerformanceResult");
		Zend_Loader::loadClass("ReceiptReference");
		
		$supplierHead = new SupplierPerformance();
		$supplierDetail = new SupplierPerformanceResult();
		$poref = new ReceiptReference();
		
		foreach($head as $key => $value) {
			
			$poref->clear();
			$poref->loadWhere("user_id  = ". $userid . " and receipt_id = ".$value[3]);
		
			$supplierHead->clear();
			$supplierHead->evaluator_org_id = $value[1];
			$supplierHead->supplier_org_id = $value[2];
			$supplierHead->reference_receipt_id = $poref->new_receipt_id;
			$supplierHead->date_evaluated = $value[4];
			$supplierHead->overall_rate = $value[5];
			$supplierHead->date_sent = $value[6];
			$supplierHead->date_created = $value[7];
			$supplierHead->date_updated = $value[8];
			$supplierHead->created_by = $value[9];
			$supplierHead->updated_by = $value[10];
			$supplierHead->comments = $value[11];
			$supplierHead->save();
			
			foreach($detail[$key] as $dkey => $dvalue) {
				$supplierDetail->clear();
				$supplierDetail->ssp_id = $supplierHead->ssp_id;
				$supplierDetail->doa = $dvalue[2];
				$supplierDetail->unordered_organism = $dvalue[3];
				$supplierDetail->misidentified_organism = $dvalue[4];
				$supplierDetail->size = $dvalue[5];
				$supplierDetail->water_quantity = $dvalue[6];
				$supplierDetail->packing = $dvalue[7];
				$supplierDetail->date_created = $dvalue[8];
				$supplierDetail->date_updated = $dvalue[9];
				$supplierDetail->created_by = $dvalue[10];
				$supplierDetail->updated_by = $dvalue[11];
				$supplierDetail->save();
			}
			
		}	
		
	}
	
	public function formatexport($directory,$data) {
		
		//print_r($data);die();
		Zend_Loader::loadClass("purchaseOrder");
		$po = new purchaseOrder();
		$filename = $directory."orders.txt";
		$handle = fopen($filename, 'w+');
		fwrite($handle,"Orders");
		fwrite($handle,"\n");
		foreach($data['head'] as $key => $value) {
		
			$po->clear();
			$po->loadWhere(" po_id = ".$value['po_id']);
			$po->exported=1;
			$po->save();
			
			fwrite($handle,implode(",",$value)."\n");
		}
		fwrite($handle,"Order Details");
		fwrite($handle,"\n");
		foreach($data['detail'] as $key => $value) {
			fwrite($handle,implode(",",$value)."\n");
		}
		
		
		
	}
	
	
	
 }